package com.jorado.apollo.service;

import com.jorado.apollo.dto.AppNamespaceDTO;
import org.apache.http.impl.client.CloseableHttpClient;

public class NamespaceApiService extends AbstractApiService {

    public NamespaceApiService(CloseableHttpClient client, String baseUrl) {
        super(client, baseUrl);
    }

    public void createNamespace(AppNamespaceDTO appNamespaceDTO) {

        String path = String.format("apps/%s/appnamespaces", escapePath(appNamespaceDTO.getAppId()));

        try {
            post(path, appNamespaceDTO);
        } catch (Throwable ex) {
            throw new RuntimeException(String.format("create namespace for appId: %s failed", appNamespaceDTO.getAppId()), ex);
        }
    }

    public void deleteNamespace(String appId, String namespaceName) {

        checkNotEmpty(appId, "App id");
        checkNotEmpty(namespaceName, "namespaceName");
        String path = String.format("apps/%s/appnamespaces/%s", escapePath(appId), escapePath(namespaceName));

        try {
            delete(path);
        } catch (Throwable ex) {
            throw new RuntimeException(String.format("deleted namespace for appId: %s, namespace: %s  failed", appId, namespaceName), ex);
        }
    }
}
